//
//  SystemSettingViewController.m
//  PigMarket
//
//  Created by mac on 2019/3/20.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "SystemSetingViewController.h"
#import "SystemSetingCell.h"
#import "AccountSecurityViewController.h"
#import "ChangeAccountViewController.h"
#import "ComplaintFeedbackViewController.h"
#import "AboutUsViewController.h"

@interface SystemSetingViewController ()

@property (nonatomic , strong) NSArray *dataArr;

@end


@implementation SystemSetingViewController

#pragma mark - view life cycle

- (void)setupUI{
    
    self.baseNavView.navTitleLabel.text = @"设置";
    self.tableView.separatorColor = mIconLineColor;
    self.tableView.rowHeight = 54.f;
}


- (void)setupLayout{
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(TJ_NavBarHeight);
        make.left.right.bottom.mas_equalTo(self.view);
    }];
}


- (void)initData {
    
    self.dataArr = @[@[@{@"title":@"账号安全",@"detail":@""}],
                     @[@{@"title":@"意见反馈",@"detail":@"功能异常或反馈"},
                       @{@"title":@"关于我们",@"detail":@""}],
                     @[@{@"title":@"清除缓存",@"detail": [self getSDWebImageCacheCount]},
                       @{@"title":@"切换账号",@"detail":@""}],
                     @[@{@"title":@"退出登录",@"detail":@""}]];
}


#pragma mark - private method

- (void)clearLoginAccount {
    
    [self toastWithContent:@"测试临时关闭退出聊天功能"];
    [[UserInfoDataProvider sharedInstance] logoutPersonal];
    [self toastWithContent:@"退出成功"];
    [self.navigationController popToRootViewControllerAnimated:YES];
}


- (NSString *)getSDWebImageCacheCount
{
    NSUInteger tmpSize = [[SDImageCache sharedImageCache] getSize];
    
    NSString *clearCacheName;
    if (tmpSize >= 1024*1024*1024) {
        clearCacheName = [NSString stringWithFormat:@"%0.2fG",tmpSize /(1024.f*1024.f*1024.f)];
    }else if (tmpSize >= 1024*1024) {
        clearCacheName = [NSString stringWithFormat:@"%0.2fM",tmpSize /(1024.f*1024.f)];
    }else{
        clearCacheName = [NSString stringWithFormat:@"%0.2fK",tmpSize / 1024.f];
    }
    return clearCacheName;
}


#pragma mark - delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.dataArr[section] count];
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SystemSetingCell *cell = [SystemSetingCell cellWithTableView:tableView];
    cell.infoDic = self.dataArr[indexPath.section][indexPath.row];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    switch (indexPath.section) {
        case 0: //账号安全
            {
                AccountSecurityViewController *accountSecurityVC = [[AccountSecurityViewController alloc] init];
                [self.navigationController pushViewController:accountSecurityVC animated:YES];
            }
            break;
        case 1:
        {
            switch (indexPath.row) {
                case 0: //意见反馈
                {
                    ComplaintFeedbackViewController *complaintFeedbackVC = [[ComplaintFeedbackViewController alloc] init];
                    [self.navigationController pushViewController:complaintFeedbackVC animated:YES];
                }
                    break;
                case 1://帮助
                {
                    AboutUsViewController *aboutUsVC = [[AboutUsViewController alloc] init];
                    [self.navigationController pushViewController:aboutUsVC animated:YES];
                }
                    break;
                default:
                    break;
            }

        }
            break;
        case 2:
        {
            switch (indexPath.row) {
                case 0://清除缓存
                {
                    kWeakSelf(self);
                    [self clearMemoryBByController:self callBack:^{
                        [weakself initData];
                        [weakself.tableView reloadData];
                    }];
                }
                    break;
                case 1://切换账号
                {
                    ChangeAccountViewController *changeAccountVC = [[ChangeAccountViewController alloc] init];
                    [self.navigationController pushViewController:changeAccountVC animated:YES];
                }
                    break;
                default:
                    break;
            }
        }
            break;
            
        default: //退出登录
        {
            @weakify(self);
            [[AlertAction sharedInstance] showAlertWithTitle:nil message:@"退出登录后将无法继续操作" confirmBtnTitle:@"确定" confirmBlock:^{
                @strongify(self);
                [self clearLoginAccount];
            } cancelBlock:nil];

        }
            break;
    }
}


#pragma mark - getter and setter

- (NSArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSArray alloc] init];
    }
    return _dataArr;
}

@end


